<?php
// +----------------------------------------------------------------------
// | Description: about us
// +----------------------------------------------------------------------
// | Author: orchief
// +----------------------------------------------------------------------
// | Date: 2018-03-06 13:37:11
// +----------------------------------------------------------------------

namespace App\Model;

use App\Common\Model;
use think\Db;
use App\Utility\Extend\Tree;

class User extends Model
{
    protected $name = 'admin_user';
    protected $insert = [
        'status' => 1,
    ];
    protected $tableName = 'admin_user';
    protected $page; // 当前页码
    protected $limit = 15; // 每页数据
    protected $fuzzyConditions = ['username', 'remark']; // 模糊查询字段
    protected $clearConditions = ['status', 'role']; // 精确查询字段
    protected $rangeConditions = ['create_time'];
    protected $union_fuzzyConditions = null;
    protected $foreignConditions = null;
    protected $returnFields = 'id,username,remark,status,create_time';

      /**
     * 解析搜索条件
     */
    public function getDataList($param)
    {
        $this->param = $param;
        if (isset($this->param['Page'])) {
            $this->page = $this->param['Page'];
        }
        if (isset($this->param['Limit'])) {
            $this->limit = $this->param['Limit'];
        }
        if (isset($this->param['OrderByString'])) {
            $this->orderByString = $this->param['OrderByString'];
        }
        // 数据
        $Model = $this;
        $Model = $this->parseParam($Model)->where('role', 0);
        $Model = $Model->field($this->returnFields);
        if ($this->limit != -1) {
            $Model = $Model->page($this->page, $this->limit);
        }
        $resData = $Model->select();
        // 数量
        $Model = $this;
        $Model = $this->parseParam($Model)->where('role', 0);
        $dataCount = $Model->count();
        $data['list'] = $resData;
        $data['dataCount'] = $dataCount;
        return $this->filter($data);
    }

    /**
     * 获取用户所属所有用户组
     * @param  array   $param  [description]
     */
    public function groups()
    {
        return $this->belongsToMany('group', 'admin_access', 'group_id', 'user_id');
    }

    /**
     * [getDataById 根据主键获取详情]
     * @linchuangbin
     * @DateTime  2017-02-10T21:16:34+0800
     * @param     string                   $id [主键]
     * @return    [array]
     */
    public function getDataById($id = '')
    {
        $data = $this->get($id);
        if (!$data) {
            $this->error = '暂无此数据';
            return false;
        }
        $data['groups'] = $this->get($id)->groups;
        return $data;
    }
    /**
     * 创建用户
     * @param  array   $param  [description]
     */
    public function createData($param, $field = true)
    {
        if (empty($param['groups'])) {
            $this->error = '请至少勾选一个用户组';
            return false;
        }

        // 验证
        $validateName = 'App\\Validate\\'.$this->convertUnderline($this->name);
        $validate = new $validateName();
        if (!$validate->scene('Create')->check($param)) {
            $this->error = $validate->getError();
            return false;
        }

        $this->startTrans();
        try {
            $param['password'] = user_md5($param['password']);
            $this->data($param)->allowField($field)->save();

            foreach ($param['groups'] as $k => $v) {
                $userGroup['user_id'] = $this->id;
                $userGroup['group_id'] = $v;
                $userGroups[] = $userGroup;
            }
            Db::name('admin_access')->insertAll($userGroups);

            $this->commit();
            return true;
        } catch (\Exception $e) {
            $this->rollback();
            $this->error = '添加失败';
            return false;
        }
    }

    /**
     * 通过id修改用户
     * @param  array   $param  [description]
     */
    public function updateDataById($param, $id, $fields = true, $pk = 'Id')
    {
        // 不能操作超级管理员
        if ($id == 1) {
            $this->error = '非法操作';
            return false;
        }
        $checkData = $this->get($id);
        if (!$checkData) {
            $this->error = '暂无此数据';
            return false;
        }
        if (empty($param['groups'])) {
            $this->error = '请至少勾选一个用户组';
            return false;
        }
        $this->startTrans();

        try {
            Db::name('admin_access')->where('user_id', $id)->delete();
            foreach ($param['groups'] as $k => $v) {
                $userGroup['user_id'] = $id;
                $userGroup['group_id'] = $v;
                $userGroups[] = $userGroup;
            }
            Db::name('admin_access')->insertAll($userGroups);

            if (!empty($param['password'])) {
                $param['password'] = user_md5($param['password']);
            } else {
                unset($param['password']);
            }
            $this->allowField(true)->save($param, ['id' => $id]);
            $this->commit();
            return true;

        } catch (\Exception $e) {
            $this->rollback();
            $this->error = '编辑失败';
            return false;
        }
    }

    /**
     * [login 登录]
     * @AuthorHTL
     * @DateTime  2017-02-10T22:37:49+0800
     * @param     [string]                   $u_username [账号]
     * @param     [string]                   $u_pwd      [密码]
     * @param     [string]                   $verifyCode [验证码]
     * @param     Boolean                       $isRemember [是否记住密码]
     * @param     Boolean                    $type       [是否重复登录]
     * @return    [type]                               [description]
     */
    public function login($username, $password, $verifyCode = '', $isRemember = false, $type = false)
    {
        if (!$username) {
            $this->error = '帐号不能为空';
            return false;
        }
        if (!$password) {
            $this->error = '密码不能为空';
            return false;
        }
        if (config('IDENTIFYING_CODE') && !$type) {
            if (!$verifyCode) {
                $this->error = '验证码不能为空';
                return false;
            }
            $captcha = new HonrayVerify(config('captcha'));
            if (!$captcha->check($verifyCode)) {
                $this->error = '验证码错误';
                return false;
            }
        }

        $map['username'] = $username;
        $userInfo = $this->where($map)->find();
        if (!$userInfo) {
            $this->error = '帐号不存在';
            return false;
        }
        if (user_md5($password) !== $userInfo['password']) {
            $this->error = '密码错误';
            return false;
        }
        if ($userInfo['status'] == 0) {
            $this->error = '帐号已被禁用';
            return false;
        }
        // 获取菜单和权限
        $dataList = $this->getMenuAndRule($userInfo['id']);
        if (!$dataList['menusList']) {
            $this->error = 'Permission denied!';
            return false;
        }

        if ($isRemember || $type) {
            $secret['username'] = $username;
            $secret['password'] = $password;
            $data['rememberKey'] = encrypt($secret);
        }

        // 保存缓存
        session_start();
        $info['userInfo'] = $userInfo;
        $info['sessionId'] = session_id();
        $authKey = user_md5($userInfo['username'] . $userInfo['password'] . $info['sessionId']);
        $info['_AUTH_LIST_'] = $dataList['rulesList'];
        $info['authKey'] = $authKey;
        cache('Auth_' . $authKey, null);
        cache('Auth_' . $authKey, $info, config('LOGIN_SESSION_VALID'));
        // 返回信息
        $data['authKey'] = $authKey;
        $data['sessionId'] = $info['sessionId'];
        $data['userInfo'] = $userInfo;
        $data['authList'] = $dataList['rulesList'];
        $data['menusList'] = $dataList['menusList'];
        return $data;
    }

    /**
     * 修改密码
     * @param  array   $param  [description]
     */
    public function setInfo($auth_key, $old_pwd, $new_pwd)
    {
        $cache = cache('Auth_' . $auth_key);
        if (!$cache) {
            $this->error = '请先进行登录';
            return false;
        }
        if (!$old_pwd) {
            $this->error = '请输入旧密码';
            return false;
        }
        if (!$new_pwd) {
            $this->error = '请输入新密码';
            return false;
        }
        if ($new_pwd == $old_pwd) {
            $this->error = '新旧密码不能一致';
            return false;
        }

        $userInfo = $cache['userInfo'];
        $password = $this->where('id', $userInfo['id'])->value('password');
        if (user_md5($old_pwd) != $password) {
            $this->error = '原密码错误';
            return false;
        }
        if (user_md5($new_pwd) == $password) {
            $this->error = '密码没改变';
            return false;
        }
        if ($this->where('id', $userInfo['id'])->setField('password', user_md5($new_pwd))) {
            $userInfo = $this->where('id', $userInfo['id'])->find();
            // 重新设置缓存
            session_start();
            $cache['userInfo'] = $userInfo;
            $cache['authKey'] = user_md5($userInfo['username'] . $userInfo['password'] . session_id());
            cache('Auth_' . $auth_key, null);
            cache('Auth_' . $cache['authKey'], $cache, config('LOGIN_SESSION_VALID'));
            return $cache['authKey']; //把auth_key传回给前端
        }

        $this->error = '修改失败';
        return false;
    }

    /**
     * 获取菜单和权限
     * @param  array   $param  [description]
     */
    protected function getMenuAndRule($u_id)
    {
        if ($u_id == 1) {
            $map['status'] = 1;
            $menusList = Db::name('admin_menu')->where($map)->order('sort asc')->select();
        } else {
            $groups = $this->get($u_id)->groups;
            $ruleIds = [];
            foreach ($groups as $k => $v) {
                $ruleIds = array_unique(array_merge($ruleIds, explode(',', $v['rules'])));
            }

            $ruleMap['id'] = array('in', $ruleIds);
            $ruleMap['status'] = 1;
            // 重新设置ruleIds，除去部分已删除或禁用的权限。
            $rules = Db::name('admin_rule')->where($ruleMap)->select();
            foreach ($rules as $k => $v) {
                $ruleIds[] = $v['id'];
                $rules[$k]['name'] = strtolower($v['name']);
            }
            empty($ruleIds) && $ruleIds = '';
            $menuMap['status'] = 1;
            $menuMap['rule_id'] = array('in', $ruleIds);
            $menuModel = Db::name('admin_menu');
            $menusList = $menuModel->where($menuMap)->order('sort asc')->select();
        }
        if (!$menusList) {
            return null;
        }

        //处理菜单成树状
        $tree = new Tree();
        $ret['menusList'] = $tree->list_to_tree($menusList, 'id', 'pid', 'child', 0, true, array('pid'));

        $ret['menusList'] = memuLevelClear($ret['menusList']);
        // 处理规则成树状
        $ret['rulesList'] = $tree->list_to_tree($rules, 'id', 'pid', 'child', 0, true, array('pid'));
        $ret['rulesList'] = rulesDeal($ret['rulesList']);
        return $ret;
    }
}
